package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R
import com.polaris.live.databinding.WidgetStateBinding
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.translation.R.string
import com.polaris.live.utils.state.UserStateUtils
import com.polaris.live.utils.state.UserStateUtils.UserStatus

/**
 * StateView
 *
 * @author Created by lucas on 2023/11/7 09:52
 */
class StateView(context: Context, attributeSet: AttributeSet) :
    LinearLayout(context, attributeSet) {

    private val mBinding = WidgetStateBinding.inflate(LayoutInflater.from(context), this)

    init {
        setBackgroundResource(R.drawable.hot_shape_item_back_round)
        orientation = HORIZONTAL
    }

    /**
     * 设置状态，通过用户基本信息
     */
    fun setState(userBasic: UserBasicVo, displayUserBean: DisplayUserBean? = null, textSize: Float? = null) {
        setState(UserStateUtils.resolveListUserState(userBasic), userBasic.partyGameType, displayUserBean?.gameName)

        textSize?.let {
            mBinding.tvState.textSize = it
        }
    }

    /**
     * 设置状态
     */
    fun setState(@UserStatus state: Int, partyGameType: Int? = null, gameName: String? = null) {
        mBinding.apply {
            when (state) {
                UserStateUtils.USER_STATE_ONLINE, UserStateUtils.USER_STATE_OFFLINE, UserStateUtils.USER_STATE_BUSY -> {
                    vState.visibility = View.VISIBLE
                    ivStateLive.visibility = View.GONE
                    tvState.text = ""
                    when (state) {
                        UserStateUtils.USER_STATE_ONLINE -> {
                            vState.setBackgroundResource(R.drawable.hot_shape_item_dot_green)
                            tvState.text = StringUtils.getString(string.Online)
                        }

                        UserStateUtils.USER_STATE_OFFLINE -> {
                            vState.setBackgroundResource(R.drawable.hot_shape_item_dot_grey)
                            tvState.text = StringUtils.getString(string.Offline)
                        }

                        UserStateUtils.USER_STATE_BUSY -> {
                            vState.setBackgroundResource(R.drawable.hot_shape_item_dot_red)
                            tvState.text = StringUtils.getString(string.Busy)
                        }

                        else -> {
                            vState.visibility = View.GONE
                            ivStateLive.visibility = View.GONE
                        }
                    }
                }

                UserStateUtils.USER_STATE_LIVE, UserStateUtils.USER_STATE_MATCH, UserStateUtils.USER_STATE_PARTY, UserStateUtils.USER_STATE_PARTY_VIDEO -> {
                    vState.visibility = View.GONE
                    ivStateLive.visibility = View.VISIBLE
                    tvState.text = ""
                    when (state) {
                        UserStateUtils.USER_STATE_LIVE -> {
                            ivStateLive.setImageResource(null)
                            tvState.text = StringUtils.getString(string.Live)
                        }

                        UserStateUtils.USER_STATE_MATCH -> {
//                            ivStateLive.setImageResource(R.drawable.icon_list_match)
//                            tvState.text = StringUtils.getString(string.match)
                            vState.setBackgroundResource(R.drawable.hot_shape_item_dot_green)
                            tvState.text = StringUtils.getString(string.Online)
                            vState.visibility = View.VISIBLE
                        }

                        UserStateUtils.USER_STATE_PARTY -> {
                            ivStateLive.setImageResource(R.drawable.icon_party)
                            tvState.text = StringUtils.getString(string.party)
                        }

                        UserStateUtils.USER_STATE_PARTY_VIDEO -> {
                            ivStateLive.setImageResource(R.drawable.icon_party_video)
                            tvState.text = StringUtils.getString(string.video_party)
                        }

                        else -> {
                            vState.visibility = View.GONE
                            ivStateLive.visibility = View.GONE
                        }
                    }
                }

                UserStateUtils.USER_GAME_WAITING, UserStateUtils.USER_GAME_PLAYING -> {
                    vState.visibility = View.VISIBLE
                    ivStateLive.visibility = View.GONE
                    tvState.text = ""
                    if (partyGameType == null) {
                        vState.setBackgroundResource(R.drawable.hot_shape_item_dot_red)
                        tvState.text = StringUtils.getString(string.Busy)
                        return
                    }
                    when (state) {
                        UserStateUtils.USER_GAME_WAITING -> {
                            vState.setBackgroundResource(R.drawable.hot_shape_item_dot_red)
                            val showGameName = gameName.takeIf { it?.isNotEmpty() == true } ?: ""
                            tvState.text = "$showGameName ${StringUtils.getString(string.waiting)}"
                        }

                        UserStateUtils.USER_GAME_PLAYING -> {
                            vState.setBackgroundResource(R.drawable.hot_shape_item_dot_red)
                            val showGameName = gameName.takeIf { it?.isNotEmpty() == true } ?: ""
                            tvState.text = "$showGameName ${StringUtils.getString(string.playing)}"
                        }

                        else -> {
                            vState.visibility = View.GONE
                            ivStateLive.visibility = View.GONE
                        }
                    }

                }

                else -> {
                    vState.visibility = View.GONE
                    ivStateLive.visibility = View.GONE
                }
            }
        }
    }
}